package messageprocessors.clients;

import java.util.HashMap;
import java.util.Map;
import java.util.Observer;

import org.apache.log4j.Logger;

import util.DSPropertyManager;

import messages.DSMessage;
import messages.DSMessageType;

public class Terminal extends BaseClient {
	
	private static Logger log = Logger.getLogger(Terminal.class);

	// read xmpp id and password from properties file
	private static String networkid = DSPropertyManager.getInstance()
			.getStringValue("remoteclientid", "remoteclient");
	private static String networkpw = DSPropertyManager.getInstance()
			.getStringValue("remoteclientpassword", "remoteclient");

	public Terminal() {
		this(networkid, networkpw);
	}

	public Terminal(Observer ob) {
		this(networkid, networkpw, ob);
	}

	public Terminal(String id, String xmppPassword) {
		super(id, xmppPassword);
		// TODO Auto-generated constructor stub
	}

	public Terminal(String id, String xmppPassword, Observer ob) {
		this(id, xmppPassword);
		this.addObserver(ob);
	}

	public void constructmessageRequest(String target) throws Exception {
		DSMessage message = new DSMessage();
		message.setTargetId(target);
		message.setOriginId("Terminal");
		message.setType(DSMessageType.STATUSREQUEST);
		// sendMessage(message,target);
		System.out.println(message);

	}

	public void constructmessageCommand(String target, Map<String, String> map)
			throws Exception {

		DSMessage message = new DSMessage(map);
		message.setTargetId(target);
		message.setOriginId("Terminal");
		message.setType(DSMessageType.COMMAND);
		// sendMessage(message,target);
		System.out.println(message);

	}

	@Override
	public void processMessage(DSMessage message) throws Exception {
		log.debug("Terminal received: " + message);

		if (message == null) {
			log.debug("Retrieved null message!");
			throw new Exception("The received message is null!");
		}

		// Check for errors ---> handle errors
		if (message.getError() != null) {
			this.setLastErrorRetrieved(message.getError());
		}
		// status request message
		if (message.getType() == DSMessageType.STATUSREQUEST) {
			processStatusRequestMessage(message);

		}
		// command message
		else if (message.getType() == DSMessageType.COMMAND) {
			processCommandMessage(message);
		}
		// datenrequest message
		else if (message.getType() == DSMessageType.DATEN) {
				processDatenMessage(message);
		}
		// status message
		else if (message.getType() == DSMessageType.STATUS) {
			processStatusMessage(message);
		}
		reportStatus(message.getOriginId());
		this.setLastMessageReceived(message);
	}

	private void processDatenMessage(DSMessage message) {
		// TODO Auto-generated method stub
		
	}

	private void processStatusMessage(DSMessage message) {
		
	}

	private void processCommandMessage(DSMessage message) {
		// TODO Auto-generated method stub

	}

	private void processStatusRequestMessage(DSMessage message) {
		// TODO Auto-generated method stub

	}

	@Override
	protected Map<String, String> generatePropertyMap() {
		return null;
	}

}
